﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IDAL;
using Model;

namespace SQLServerDAL
{

    public class LoginLogDAL : ILoginLogDAL
    {
         public Boolean InsertLoginLog(LoginLog loginLog) 
        {
            try
            {
                LoginLogDataClassesDataContext loginLogContext = new LoginLogDataClassesDataContext();
                loginLogContext.LoginLogs.InsertOnSubmit(loginLog);
                loginLogContext.SubmitChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }


        public Boolean DeleteLoginLog(int loginId) 
        {
            try
            {
                LoginLogDataClassesDataContext loginLogContext = new LoginLogDataClassesDataContext();
                LoginLog loginLog = (from u in loginLogContext.LoginLogs
                             where u.LoginId.Equals(loginId)
                             select u).First();
                loginLogContext.LoginLogs.DeleteOnSubmit(loginLog);
                loginLogContext.SubmitChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }


        public Boolean UpdateLoginLog(int loginId,LoginLog loginLog) 
        {
            
                if (loginId != loginLog.LoginId) {
                    return false;
                }
                LoginLogDataClassesDataContext loginLogContext = new LoginLogDataClassesDataContext();
                LoginLog query = (from u in loginLogContext.LoginLogs
                             where u.LoginId.Equals(loginId)
                             select u).First();
                //query.Password = loginLog.Password;
                
                loginLogContext.SubmitChanges();
                return true;
            
        }

        public LoginLog SelectLoginLogById(int loginId)
        {
            LoginLogDataClassesDataContext loginLogContext = new LoginLogDataClassesDataContext();
            LoginLog loginLog = (from u in loginLogContext.LoginLogs
                         where u.LoginId.Equals(loginId)
                         select u).First();
            return loginLog;
        }


        public List<LoginLog> SelectAllLoginLog() 
        {
            LoginLogDataClassesDataContext loginLogContext = new LoginLogDataClassesDataContext();
            var query = (from u in loginLogContext.LoginLogs
                         select u);
            List<LoginLog> loginLogList = new List<LoginLog>();

            foreach (var loginLog in query) {
                loginLogList.Add(loginLog);
            }
            return loginLogList;
        }

    }
}
